<?php

namespace app\admin\controller;

use common\Tree;
use think\facade\Db;
use app\admin\model\AuthGroup;
use app\admin\model\AuthNodes;
use think\facade\View;
use common\Nodes;

class Auth extends Common
{

    public function __construct()
    {
        parent::__construct();
    }
	
    /**
     * 权限节点列表
     * @return array
     * @author admin
     * @date 2019-08-12
     */
	 
    public function index()
    {
        if (request()->isPost()) {
            $tree = new Tree();
            $arr = Nodes::lists();
            $arr = $tree->menu($arr); //格式化
            return ['code' => 0, 'msg' => '获取成功！', 'data' => $arr, 'rel' => 1];
        }
        return view();
    }

    /**
     * 添加权限节点
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['addtime'] = time();
                AuthNodes::create($data);
                cache('admin_left_menu_userid_' . session(config("auth.USER_AUTH_KEY")), null);
                return ['code' => 0, 'msg' => '权限添加成功！'];
            } else {
                $Tree = new Tree();
                $lists = Nodes::lists(1);
                foreach ($lists as $key => $one) {
                    if ($one['level'] != 1 && $one['level'] != 2) {
                        unset($lists[$key]);
                    }
                }
                $arr = $Tree->menu($lists);
                $this->assign('navList', $arr);//权限列表
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 编缉节点
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $datas = input('post.');
                if (AuthNodes::update($datas)) {
                    cache('admin_left_menu_userid_' . session(config("auth.USER_AUTH_KEY")), null);
                    return ['code' => 0, 'msg' => '保存成功！'];
                } else {
                    return ['code' => 1, 'msg' => '保存失败！'];
                }
            } else {
                $id = input("id", 0, 'intval');
                if (!$id) {
                    return ['code' => 1, 'msg' => '非法请求！'];
                }
                $Tree = new Tree();
                $lists = Nodes::lists();
                foreach ($lists as $key => $one) {
                    if ($one['level'] != 1 && $one['level'] != 2) {
                        unset($lists[$key]);
                    }
                }
                $arr = $Tree->menu($lists);
                $this->assign('navList', $arr);//权限列表
                $admin_rule = Db::name("auth_nodes")->field("id,pid,name,title,icon,sort,menustatus,url")->where("id", $id)->find();
                $this->assign('rule', $admin_rule);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 列表动态修改权限
     * @return array
     * @author admin
     * @date 2019-08-12
     */

    public function listEdit()
    {
        try {
            $data = input('param.');
            if (AuthNodes::update($data)) {
                return ['code' => 0, 'msg' => '修改成功！'];
            } else {
                return ['code' => 1, 'msg' => '修改失败！'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 删除权限节点
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function doDelete()
    {
        try {
            $id = input('post.id');
            $this->delauth($id);
            return ['code' => 0, 'msg' => '删除成功！'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 如果权限节点有子节点，则一并删除子节点
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    private function delauth($id)
    {
        try {
            AuthNodes::destroy($id);
            $authSon = Db::name('auth_nodes')->where('pid', $id)->select()->toArray();
            if (!empty($authSon)) {
                foreach ($authSon as $k => $v) {
                    $this->delauth($v['id']);
                }
            }
            return true;
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 设置权限菜单显示或者隐藏
     * @return array
     * @author admin
     * @date 2019-08-12
     */
    public function doStatus()
    {
        try {
            $where['id'] = input('post.id');
            $data['menustatus'] = input('post.menustatus');
            if (AuthNodes::update($data, $where)) {
                return ['code' => 0, 'msg' => '设置成功！'];
            } else {
                return ['code' => 1, 'msg' => '设置失败！'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

}